<?php

/**
 * otherprofile actions.
 *
 * @package    LYPO
 * @subpackage otherprofile
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 9301 2008-05-27 01:08:46Z dwhittle $
 */
class otherprofileActions extends sfActions
{
public function preExecute(){
		if(sfConfig::get('sf_web_debug')==true){
			$this->setLayout('layout');
		}else{
			$this->setLayout(false);
		}
		$this->setTemplate('default');
	}

	/**
	 * Executes myprofile action
	 *
	 * @param sfRequest $request A request object
	 */
	public function executeProfile(sfWebRequest $request)
	{
		$result = new LypoResult();
		$userid = $request->getParameter('userid');
		$profile_userid = $request->getParameter('profile_userid');
		if(!$userid || !$profile_userid){
			$result->setErrorMessage("No userid or profile_userid given!");
			return $this->renderText(print_r($result->toArray(),1));
		}
		try {
			/* @var $user User */
			$user = UserPeer::getByContainerString($userid);
			$profile_user = UserPeer::retrieveByPK($profile_userid);
			if($user && $profile_user){
				if (!$profile_user->getShowProfile()) return sfView::NONE;

				$this->args = array('user' => $user, 'profile_user' => $profile_user, 'userid' => $userid, 'profile_userid' => $profile_userid);
				
				//Haal alle opposite ids op voor ingleogde gebruiker, zodat met in-array gematched kan worden 
				//Of deze gebruiker een perfect opposite is!
				$opposite_ids = $user->getPerfectOppositesIds(10);
				$this->args['opposite_ids'] = $opposite_ids['ids'];
				
				$this->partial = 'profile';
			}else{
				$result->setErrorMessage("User or profile user not found!");
				return $this->renderText(print_r($result->toArray(),1));
			}
		}catch( Exception $e){
			$result->setErrorMessage($e->getMessage());
			return $this->renderText(print_r($result->toArray(),1));
		}
	}
	
/**
	 * Executes myprofile action
	 *
	 * @param sfRequest $request A request object
	 */
	public function executeStatements(sfWebRequest $request)
	{
		$result = new LypoResult();
		$userid = $request->getParameter('userid');
		$profile_userid = $request->getParameter('profile_userid');
		$statement_page =   $this->getRequestParameter('statement_page',1);
		$debate_page =   $this->getRequestParameter('debate_page',1);
		$active_tab  = $this->getRequestParameter('active');
		$active_tab  = $active_tab != '' ? $active_tab : 'statements';

		if(!$userid || !$profile_userid){
			$result->setErrorMessage("No userid or profile_userid given!");
			return $this->renderText(print_r($result->toArray(),1));
		}
		try {
			/* @var $user User */
			$user = UserPeer::getByContainerString($userid);
			$profile_user = UserPeer::retrieveByPK($profile_userid);
			if($user){
				if (!$profile_user->getShowProfile()) return sfView::NONE;
				$statement_answers = StatementAnswerPeer::getUserRegistrationAnswersPager($profile_user,$statement_page,5);
				$mydebatereactions = LypoStatementPeer::getUserDebatesReactionsPager($profile_user,$debate_page,5);
				$this->args = array('user' => $user, 'profile_userid' => $profile_userid, 'active_tab' => $active_tab, 'statement_answers' => $statement_answers, 'userid' => $userid, 'mydebatereactions' => $mydebatereactions);
				if($mydebatereactions){
					$myanswers = StatementAnswerPeer::getUserAnswersIds($profile_user);
					$this->args['myanswers'] = $myanswers;
				}
				$this->partial = 'statements';
			}else{
				$result->setErrorMessage("Profile user not found");
				return $this->renderText(print_r($result->toArray(),1));
			}
		}catch( Exception $e){
			$result->setErrorMessage($e->getMessage());
			return $this->renderText(print_r($result->toArray(),1));
		}
	}		
	
	/**
	 * Executes index action
	 *
	 * @param sfRequest $request A request object
	 */
	public function executeOpposites(sfWebRequest $request)
	{
		$result = new LypoResult();
		$userid = $request->getParameter('userid');
		$profile_userid = $request->getParameter('profile_userid');
		if(!$userid || !$profile_userid){
			$result->setErrorMessage("No userid or profile_userid given!");
			return $this->renderText(print_r($result->toArray(),1));
		}		
		try {
			/* @var $user User */
			$user = UserPeer::getByContainerString($userid);
			$profile_user = UserPeer::retrieveByPK($profile_userid);
			if($user && $profile_user){
				if (!$profile_user->getShowProfile()) return sfView::NONE;
				$this->args = array('user' => $user, 'profile_user' => $profile_user);
				$this->partial = 'opposites';
				//return $this->renderPartial('opposites',array('user' => $user));
			}else{
				$result->setErrorMessage("User not found");
				return $this->renderText(print_r($result->toArray(),1));
			}
		}catch( Exception $e){
			$result->setErrorMessage($e->getMessage());
			return $this->renderText(print_r($result->toArray(),1));
		}
	}	
}
